//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal sealed class LabelSstRecord : CellValueRecord
    {
        private int _sstId;

        public LabelSstRecord(Cell cell, BiffWriterWorkbookContext context): base (cell, context)
        {
            Contract.Requires(cell.Value is string);
            Contract.Requires(((string)cell.Value).Length > 0);
            string value = (string)cell.Value; //\\ rich paragraph todo
            this._sstId = context.GetSstId(value);
        }

        public LabelSstRecord()
        {
        }

        protected override object GetValue(BiffReaderWorkbookContext context)
        {
            int index = context.StringIdToStringIndexTable[_sstId];
            return context.Workbook.SharedStrings[index];
        }

        protected override void WriteValue(BinaryWriter writer)
        {
            writer.Write(_sstId);
        }

        protected override void ReadValue(BinaryReader reader, int length)
        {
            _sstId = reader.ReadInt32();
            Contract.Assert(_sstId >= 0);
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.LabelSst; }
        }
    }
}